<template>
  <div
    v-loading="containerloading.visible"
    class="app-container"
    :element-loading-text="containerloading.text"
    element-loading-spinner="el-icon-loading"
  >
    <el-container>
      <el-aside width="220px" style="background-color: white;padding: 2px;margin-bottom: 0;">
        <!--组织结构数据-->
        <dept-project ref="deptBuildingTree" @selectBuilding="getCurrentProject" />
      </el-aside>
      <!--用户数据-->
      <el-main style="padding: 0 0 0 20px;">
        <el-form
          ref="queryForm"
          :model="queryParams1"
          :inline="true"
          label-width="68px"
        >
          <el-form-item
            size="mini"
            label=""
            prop="userName"
          >
            <el-input
              v-model="queryParams1.userName"
              placeholder="请输入用户名称"
              clearable
              prefix-icon="el-icon-search"
              style="width: 240px"
              @input="handleQuery"
            />
          </el-form-item>
          <el-form-item size="mini">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleBatchAdd"
            >批量新增
            </el-button>
          </el-form-item>
        </el-form>

        <el-table
          ref="processTableRef"
          v-loading="loading"
          highlight-current-row
          size="small"
          :data="processList"
          height="calc(100vh - 150px)"
        >
          <!-- <el-table-column
            label="营销项目"
            align="center"
            min-width="100"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="hover"
              >
                <div class="check-edit-content">{{ scope.row.curProjectName }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.sellProjectName }}</div>
              </el-popover>
            </template>
          </el-table-column>-->
          <el-table-column
            label="工程项目"
            align="center"
            min-width="100"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="hover"
              >
                <div class="check-edit-content">{{ scope.row.curProjectName }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.curProjectName }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            label="楼栋"
            align="center"
            min-width="100"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="hover"
              >
                <div class="check-edit-content">{{ scope.row.buildingName }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.buildingName }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            label="用户"
            align="center"
            min-width="80"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="hover"
              >
                <div class="check-edit-content">{{ scope.row.userName }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.userName }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="externalUserName" min-width="60">
            <template slot="header">
              <el-popover
                placement="top-start"
                width="150"
                trigger="hover"
                content="外部用户姓名"
              >
                <div slot="reference" class="ellipsis">外部用户姓名</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            label="角色"
            align="center"
            min-width="80"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="hover"
              >
                <div class="check-edit-content">{{ scope.row.roleName }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.roleName }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="60">
            <template slot="header">
              <el-popover
                placement="top-start"
                width="70"
                trigger="hover"
                content="专业类别"
              >
                <div slot="reference" class="ellipsis">专业类别</div>
              </el-popover>
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.cfspecialtype | specialType }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="250"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="showChange(scope.row)"
              >变更记录
              </el-button>
              <el-tooltip class="item" effect="dark" content="生成旧报验单的验收记录" placement="top">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-tickets"
                  @click="openGenerateDialog(scope.row)"
                >生成
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
    <!-- 修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="500px"
      append-to-body
      @close="reset"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item
          label="工程项目"
          prop="curProjectName"
        >
          <el-input
            v-model="form.curProjectName"
            placeholder="请输入"
            class="form-input"
            disabled
          />
        </el-form-item>
        <!--<el-form-item
          label="营销项目"
          prop="sellProjectName"
        >
          <el-input
            v-model="form.sellProjectName"
            placeholder="请输入"
            class="form-input"
            disabled
          />
        </el-form-item>-->
        <el-form-item
          label="楼栋"
          prop="buildingName"
        >
          <el-input
            v-model="form.buildingName"
            placeholder="请输入"
            class="form-input"
            disabled
          />
        </el-form-item>
        <el-form-item label="用户" prop="cfcheckuserid">
          <el-select
            v-model="form.cfcheckuserid"
            placeholder="请选择"
            class="form-input"
            filterable
            @change="selectUserChange"
          >
            <el-option
              v-for="type in users"
              :key="type.fid"
              :label="'【' + type.nick_name + '】- ' + type.user_name"
              :value="type.fid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="cfuserroleid">
          <el-select
            v-model="form.cfuserroleid"
            filterable
            placeholder="请选择"
            class="form-input"
          >
            <el-option
              v-for="type in roles"
              :key="type.fid"
              :label="type.roleName"
              :value="type.fid"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="外部用户姓名"
          prop="externalUserName"
        >
          <el-input
            v-model="form.externalUserName"
            disabled
            placeholder="请输入"
            class="form-input"
          />
        </el-form-item>
        <el-form-item label="专业类别">
          <el-select
            v-model="form.cfspecialtypeids"
            multiple
            clearable
            placeholder="请选择"
            class="form-input"
          >
            <el-option
              v-for="type in specialTypes"
              :key="type.fid"
              :label="type.fnameL2"
              :value="type.fid"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 批量新增用户配置 -->
    <el-dialog
      title="批量新增"
      :close-on-click-modal="false"
      :visible.sync="batchOpen"
      width="700px"
      append-to-body
      @close="cancelBatchForm"
    >
      <el-form
        ref="batchForm"
        :model="batchForm"
        :rules="rules"
        label-width="120px"
      >
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="building-checkbox">
              <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
              <el-checkbox-group v-model="batchForm.cfbuildingids" @change="handleBuildingChange">
                <el-checkbox v-for="building in buildingList" :key="building.id" :label="building.id">{{ building.name }}</el-checkbox>
              </el-checkbox-group>
            </div>
          </el-col>
          <el-col :span="16">
            <el-form-item
              label="工程项目"
              prop="curProjectName"
            >
              <el-input
                v-model="batchForm.curProjectName"
                placeholder="请输入"
                class="form-input"
                disabled
              />
            </el-form-item>
            <!--            <el-form-item-->
            <!--              label="楼栋"-->
            <!--              prop="buildingName"-->
            <!--            >-->
            <!--              <el-input-->
            <!--                v-model="batchForm.buildingName"-->
            <!--                placeholder="请输入"-->
            <!--                class="form-input"-->
            <!--                disabled-->
            <!--              />-->
            <!--            </el-form-item>-->
            <el-form-item label="用户" prop="cfcheckuserid">
              <el-select
                v-model="batchForm.cfcheckuserid"
                placeholder="请选择"
                class="form-input"
                filterable
                @change="selectBatchUserChange"
              >
                <el-option
                  v-for="type in users"
                  :key="type.fid"
                  :label="'【' + type.nick_name + '】- ' + type.user_name"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="cfuserroleid">
              <el-select
                v-model="batchForm.cfuserroleid"
                filterable
                placeholder="请选择"
                class="form-input"
              >
                <el-option
                  v-for="type in roles"
                  :key="type.fid"
                  :label="type.roleName"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="外部用户姓名"
              prop="externalUserName"
            >
              <el-input
                v-model="batchForm.externalUserName"
                disabled
                placeholder="请输入"
                class="form-input"
              />
            </el-form-item>
            <el-form-item label="专业类别">
              <el-select
                v-model="batchForm.cfspecialtypeids"
                multiple
                clearable
                placeholder="请选择"
                class="form-input"
              >
                <el-option
                  v-for="type in specialTypes"
                  :key="type.fid"
                  :label="type.fnameL2"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          :loading="this.buttonLoading"
          @click="submitBatchForm"
        >确 定
        </el-button>
        <el-button @click="cancelBatchForm">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 变更记录 -->
    <el-dialog
      title="变更记录"
      :close-on-click-modal="false"
      :visible.sync="showChangeDialog"
      width="800px"
      append-to-body
      @close="reset"
    >
      <el-table
        :data="changeList"
        style="width: 100%"
      >
        <el-table-column
          prop="originalUsername"
          label="变更前用户"
          header-align="center"
          align="center"
          min-width="100"
        />
        <el-table-column align="center" label="前专业类别" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.originalSpecial | specialType }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="changeUsername"
          label="变更后用户"
          header-align="center"
          align="center"
          min-width="100"
        />
        <el-table-column align="center" label="新专业类别" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.changeSpecial | specialType }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="changeTime"
          label="变更时间"
          header-align="center"
          align="center"
          min-width="180"
        />
      </el-table>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="showChangeDialog = false">关闭</el-button>
      </div>
    </el-dialog>
    <!-- 用户未完成用户列表 -->
    <el-dialog
      title="未按比例验收的任务列表"
      :close-on-click-modal="false"
      :visible.sync="noFinishedTask.dialogVisible"
      width="1000px"
      append-to-body
    >
      <el-alert title="如果没有去完成任务，继续执行后续操作，可能导致后续统计报表时，验收率过低问题。" type="warning" :closable="false" />
      <h3 v-if="noFinishedTask.row">{{ noFinishedTask.row.userName }} - {{ noFinishedTask.row.roleName }}</h3>

      <el-table :data="noFinishedTask.dataList" max-height="500" style="width: 100%">
        <el-table-column
          prop="checkTypeName"
          label="分部分项"
          header-align="center"
          align="center"
          width="200"
        />
        <el-table-column
          prop="checkTableName"
          label="工序"
          header-align="center"
          align="center"
          width="220"
        />
        <el-table-column
          prop="total"
          label="施工完成"
          header-align="center"
          align="center"
        />
        <el-table-column
          prop="percent"
          label="验收比例"
          header-align="center"
          align="center"
        />
        <el-table-column
          prop="needFinish"
          label="按比例应验"
          header-align="center"
          align="center"
        />
        <el-table-column
          prop="finished"
          label="已验收"
          header-align="center"
          align="center"
        />
      </el-table>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button v-if="noFinishedTask.ob === 'delete'" type="primary" @click="continueToDo">我还要删除</el-button>
        <el-button v-if="noFinishedTask.ob === 'modify'" type="primary" @click="continueToDo">我还要修改</el-button>
        <el-button @click="noFinishedTask.dialogVisible = false">放弃</el-button>
      </div>
    </el-dialog>
    <!-- 旧报验单列表 -->
    <el-dialog
      title="根据旧报验单生成验收记录"
      :close-on-click-modal="false"
      :visible.sync="generateRecord.visiable"
      width="1000px"
      append-to-body
    >
      <el-alert
        title="提示"
        type="info"
        description="请选择需要补充生成验收记录的报验单据"
        show-icon
      />
      <el-table ref="generateTable" :data="generateRecord.inspectList" max-height="500" size="mini" style="width: 100%; margin-top:10px;" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column label="分项工程" sortable align="center" prop="checkTypeName" min-width="12%" />
        <el-table-column label="工序" sortable align="center" prop="checkTableName" min-width="13%" />
        <el-table-column label="检验批" sortable align="center" prop="batchNumber" min-width="7%" />
        <el-table-column label="检验批说明" align="center" prop="description" min-width="23%" />
        <el-table-column label="报验人" align="center" prop="inspector" min-width="7%" />
        <el-table-column label="报验时间" sortable align="center" prop="inspectionTime" min-width="15%" />
        <!--        <el-table-column label="操作时间" align="center" min-width="10%">-->
        <!--          <template slot-scope="scope">-->
        <!--            {{ scope.row.createTime | dateFilter }}-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button type="primary" :loading="generateRecord.generateFlag" @click="generate()">生成</el-button>
        <el-button @click="closeGenerateDialog()">放弃</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  addBuildingPerson,
  delBuildingPerson,
  getChangeList,
  listBuildingPerson,
  updateBuildingPerson,
  batchAddBuildingPerson,
  userList,
  getUserNoFinishedTask,
  findBeforeInspect,
  generateCheckRecord
} from '@/api/qualityManage/buildingPerson'
import { listSpecialType } from '@/api/qualityManage/checkType'
import { roleList } from '@/api/common'
import DeptProject from './components/DeptProject'

let specialTypes = []
export default {
  name: 'BuildingPerson',
  filters: {
    specialType(id) {
      if (!id) return ''
      if (!Array.isArray(id)) id = id.split(',')
      const names = []
      for (var sid of id) {
        for (let i = 0; i < specialTypes.length; i++) {
          if (specialTypes[i].fid === sid) {
            names.push(specialTypes[i].fnameL2)
          }
        }
      }
      if (!names.length) return ''
      return names.join(',')
    }
  },
  components: {
    DeptProject
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 用户表格数据
      processList: [],
      originalList: [],
      // 弹出层标题
      title: '',
      // 部门树选项
      // 是否显示弹出层
      open: false,
      buttonLoading: false,
      batchOpen: false,
      // 是否显示变更页面
      showChangeDialog: false,
      // 表单参数
      form: {
        buildingName: '',
        cfbuildingid: '',
        curProjectName: '',
        sellProjectName: '',
        cfuserroleid: '',
        cfcheckuserid: '',
        externalUserName: '',
        cfspecialtype: '',
        cfspecialtypeids: []
      },
      batchForm: {
      },
      queryParams1: {
        userName: ''
      },
      // 查询参数
      queryParams: {
        cfbuildingid: null
      },
      // 表单校验
      rules: {
        cfuserroleid: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        cfcheckuserid: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        fnameL2: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' }
        ],
        cfspecialtype: [
          { required: true, message: '请选择专业类别', trigger: 'change' }
        ]
      },
      buildingList: [],
      specialTypes: [],
      checkTypeList: [],
      currentBuilding: {},
      roles: [], // 根据用户过滤后的角色
      originalRoles: [], // 全部角色
      users: [],
      changeList: [],
      // 全选按钮
      isIndeterminate: false,
      checkAll: false,
      /* 用户未完成任务数据 */
      noFinishedTask: {
        dialogVisible: false,
        dataList: [],
        ob: null,
        row: null
      },
      /* 加载框 */
      containerloading: {
        visible: false,
        text: ''
      },
      /* 旧报验单生成验收记录 */
      generateRecord: {
        visiable: false,
        curRow: {},
        inspectList: [],
        selRows: [],
        generateFlag: false
      }
    }
  },
  created() {
    this.getSpecialList()
    this.getRoleList()
    this.getUserList()
  },
  methods: {
    /** 查询专业类别列表 */
    getSpecialList() {
      listSpecialType({}).then(response => {
        this.specialTypes = response.data
        specialTypes = response.data
        // this.getList()
      })
    },
    /** 查询角色列表 */
    getRoleList() {
      roleList({}).then(response => {
        this.originalRoles = response.data
      })
    },
    /** 查询角色列表 */
    getUserList(cb) {
      if (!this.currentBuilding.parentId) return
      userList({ 'projectId': this.currentBuilding.parentId }).then(response => {
        this.users = response.data
        if (cb) cb()
      })
    },

    /** 选择用户改变 */
    selectUserChange(userId) {
      this.roles = []
      if (userId) {
        const user = this.users.find(item => item.fid === userId)
        if (user && user.ftype === 40) {
          this.form.externalUserName = user.remark
        }
        if (user.rolefids) {
          const roleIds = user.rolefids.split(',')
          this.roles = this.originalRoles.filter(role => roleIds.includes(role.fid))
        }
        if (!this.roles.some(role => role.fid === this.form.cfuserroleid)) {
          this.form.cfuserroleid = ''
        }
      } else {
        this.form.externalUserName = ''
      }
    },
    /** 选择角色改变 */
    selectRoleChange(roleId) {
      this.form.cfcheckuserid = ''
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams1.userName = ''
      this.getList()
    },
    getCurrentProject(data) {
      if (data.id) {
        this.queryParams.cfbuildingid = data.id
        this.getList()
      } else {
        this.processList = []
        this.originalList = []
      }
      this.currentBuilding = data
      this.getUserList()
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listBuildingPerson(this.queryParams).then(
        (response) => {
          this.processList = response.data
          this.originalList = [...response.data]
          this.loading = false
          this.handleQuery()
        }
      ).catch(() => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
    },
    // 表单重置
    reset() {
      this.$refs['form'].resetFields()
    },

    handleQuery() {
      this.processList = this.originalList.filter(item => {
        return item.userName && item.userName.indexOf(this.queryParams1.userName) !== -1
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (!this.currentBuilding.id) {
        this.$message.warning('请选择楼栋')
        return
      }
      this.form = {
        buildingName: this.currentBuilding.name,
        cfbuildingid: this.currentBuilding.id,
        curProjectName: this.currentBuilding.project.name,
        sellProjectName: '',
        cfuserroleid: '',
        cfcheckuserid: '',
        externalUserName: '',
        cfspecialtype: '',
        cfspecialtypeids: []
      }
      this.open = true
      this.title = '新增用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.getUserNoFinishedTask(row, 'modify')
    },
    openUpdateDialog(row) {
      this.title = '修改用户'
      this.form = Object.assign(this.form, row)
      // 多选
      if (this.form.cfspecialtype) {
        this.form.cfspecialtypeids = this.form.cfspecialtype.split(',')
      } else {
        this.form.cfspecialtypeids = []
      }
      this.open = true
      if (!this.users.some(user => user.fid === this.form.cfcheckuserid)) {
        this.form.cfcheckuserid = ''
        this.form.cfuserroleid = ''
        this.form.externalUserName = ''
      } else {
        this.selectUserChange(this.form.cfcheckuserid)
      }
    },
    /** 批量新增 */
    handleBatchAdd() {
      this.batchForm = {
        buildingName: '',
        cfbuildingid: '',
        cfbuildingids: [],
        curProjectName: this.currentBuilding.project.name,
        sellProjectName: '',
        cfuserroleid: '',
        cfcheckuserid: '',
        externalUserName: '',
        cfspecialtype: '',
        cfspecialtypeids: []
      }
      this.buildingList = this.$refs['deptBuildingTree'].buildings
      this.batchOpen = true
    },
    /** 关闭批量新增 */
    cancelBatchForm() {
      this.batchOpen = false
    },
    /** 批量新增提交按钮 */
    submitBatchForm: function() {
      this.buttonLoading = true
      this.batchForm.cfbuildingid = this.batchForm.cfbuildingids.join(',')
      this.batchForm.cfspecialtype = this.batchForm.cfspecialtypeids.join(',')
      this.$refs['batchForm'].validate((valid) => {
        if (valid) {
          batchAddBuildingPerson(this.batchForm).then((response) => {
            if (response.code === 200) {
              this.msgSuccess('新增成功')
              this.batchOpen = false
              this.getList()
            }
            this.buttonLoading = false
          })
        }
        this.buttonLoading = false
      })
    },
    handleCheckAllChange(val) {
      var cityOptions = []
      this.buildingList.forEach(v => {
        cityOptions.push(v.id)
      })
      this.batchForm.cfbuildingids = val ? cityOptions : []
      this.isIndeterminate = false
    },
    handleBuildingChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.buildingList.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.buildingList.length
    },
    selectBatchUserChange(userId) {
      this.roles = []
      if (userId) {
        const user = this.users.find(item => item.fid === userId)
        if (user && user.ftype === 40) {
          this.batchForm.externalUserName = user.remark
        }
        if (user.rolefids) {
          const roleIds = user.rolefids.split(',')
          this.roles = this.originalRoles.filter(role => roleIds.includes(role.fid))
        }
        if (!this.roles.some(role => role.fid === this.batchForm.cfuserroleid)) {
          this.batchForm.cfuserroleid = ''
        }
      } else {
        this.batchForm.externalUserName = ''
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.cfspecialtype = this.form.cfspecialtypeids.join(',')
          if (this.form.fid !== undefined) {
            const hasExistUser = this.processList.some(item => {
              return item.cfcheckuserid === this.form.cfcheckuserid && item.fid !== this.form.fid
            })
            if (hasExistUser) {
              this.$message.error('修改楼栋用户配置失败，配置已存在')
              return
            }
            updateBuildingPerson(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addBuildingPerson(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const self = this
      this.$confirm(
        `是否要删除用户：${row.userName} 的数据项?`,
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          self.getUserNoFinishedTask(row, 'delete')
        })
        .catch(function() {
        })
    },
    deleteRow(row) {
      delBuildingPerson(row.fid).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /**
     * 显示变更记录
     * @param row
     */
    showChange(row) {
      this.changeList = []
      getChangeList(row.fid).then(res => {
        if (res.code === 200) {
          const data = res.data
          if (data.length > 0) {
            this.changeList = data
            this.showChangeDialog = true
          } else {
            this.$message.info('无变更记录')
          }
        } else {
          this.$message.error('查询变更记录一场')
        }
      })
    },
    /* 判断用户是有存在未完成的记录 */
    getUserNoFinishedTask(row, ob) {
      this.containerloading.text = '正在查询该人员是否有未完成的验收记录'
      this.containerloading.visible = true
      this.noFinishedTask.ob = {
        dialogVisible: false,
        dataList: [],
        ob: null,
        row: null }
      getUserNoFinishedTask({ buildingId: row.cfbuildingid, userFid: row.cfcheckuserid, userRoleId: row.cfuserroleid })
        .then(response => {
          if (response.code === 200) {
            this.noFinishedTask.ob = ob
            this.noFinishedTask.row = row
            if (response.data.length > 0) {
              this.noFinishedTask.dataList = response.data
              this.noFinishedTask.dialogVisible = true
            } else {
              this.continueToDo()
            }
          }
          this.containerloading.visible = false
        }).catch(r => {
          this.containerloading.visible = false
        })
    },
    /* 仍要操作 */
    continueToDo() {
      this.noFinishedTask.dialogVisible = false
      if (this.noFinishedTask.ob) {
        if (this.noFinishedTask.ob === 'modify') this.openUpdateDialog(this.noFinishedTask.row)
        else if (this.noFinishedTask.ob === 'delete') this.deleteRow(this.noFinishedTask.row)
      }
    },
    /* 打开生成任务窗口 */
    openGenerateDialog(row) {
      this.generateRecord.selRows = []
      this.generateRecord.inspectList = []
      this.generateRecord.curRow = row
      findBeforeInspect({ cfbuildingid: row.cfbuildingid, cfcheckuserid: row.cfcheckuserid, cfuserroleid: row.cfuserroleid, cfspecialtype: row.cfspecialtype }).then(response => {
        if (response.data && Array.isArray(response.data)) {
          if (response.data.length === 0) {
            this.$alert('暂无未生成验收记录的报验单列表！', {
              confirmButtonText: '确定'
            })
            return
          }
          this.generateRecord.inspectList = response.data
          this.generateRecord.visiable = true
        }
      }).catch()
    },
    handleSelectionChange(val) {
      this.generateRecord.selRows = val
    },
    /* 开始生成记录 */
    generate() {
      if (this.generateRecord.generateFlag) return
      if (!this.generateRecord.selRows || this.generateRecord.selRows.length === 0) {
        this.$message.warning('请选择需要生成的报验单')
        return
      }
      this.generateRecord.generateFlag = true
      this.containerloading.text = '正在生成验收记录，请稍后'
      this.containerloading.visible = true
      var cfinspectids = this.generateRecord.selRows.map(v => { return v.id }).join(',')
      var cfbuildingid = this.generateRecord.curRow.cfbuildingid
      var cfcheckuserid = this.generateRecord.curRow.cfcheckuserid
      var cfuserroleid = this.generateRecord.curRow.cfuserroleid

      generateCheckRecord({ cfinspectids, cfbuildingid, cfcheckuserid, cfuserroleid }).then(response => {
        this.containerloading.visible = false
        this.generateRecord.generateFlag = false
        if (response.code === 200) {
          this.$message.success('操作成功')
          this.generateRecord.visiable = false
        } else {
          this.$message.error('生成验收记录失败')
        }
      }).catch(rv => {
        this.containerloading.visible = false
        this.generateRecord.generateFlag = false
        this.$message.error('生成验收记录异常，请联系管理员')
      })
    },
    closeGenerateDialog() {
      this.generateRecord.visiable = false
      this.generateRecord.generateFlag = false
    }
  }
}
</script>
<style type="scss" scoped>
  .aside-scrollbar {
    height: calc(100vh - 150px);
  }
  .ellipsis {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
  }
  .check-content {
    max-width: 50vw;
    font-size: 12px;
    color: #606266;
  }
  .check-type {
    font-size: 16px;
    color: #606266;
    padding: 10px;
    border-bottom: 1px solid #dcdfe6;
  }
  .check-edit-content {
    max-width: 600px;
    font-size: 12px;
    color: #606266;
  }
  .form-input {
    width: 260px;
  }
</style>
<style lang="scss">
  .aside-tree-container {
    .el-scrollbar .el-scrollbar__wrap {
      overflow-x: hidden;
    }
  }
  .building-checkbox {
    height:300px;
    overflow:auto;
    .el-checkbox-group{
      .el-checkbox{
        display: block;
        margin-top: 5px;
      }
    }
  }
</style>
